package com.cbg.base.config;

import com.cbg.sys.entity.Line;
import com.cbg.sys.entity.Route;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.Map;

/**
 * <pre>redis json序列化</pre>
 * <small> 2018-10-09 | chenbing </small>
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Route> routeRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Route> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        StringRedisSerializer keySerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer(Route.class);
        template.setKeySerializer(keySerializer);
        template.setValueSerializer(valueSerializer);
        return template;
    }

    @Bean
    public RedisTemplate<String, Line> lineRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Line> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        StringRedisSerializer keySerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer valueSerializer = new Jackson2JsonRedisSerializer(Line.class);
        template.setKeySerializer(keySerializer);
        template.setValueSerializer(valueSerializer);
        return template;
    }
}
